package com.jctrip.hotel.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 */
@Slf4j
public class DateUtil {

    /**
     * 日期字符串转日期
     */
    public static Date parseDate(String dateStr, String pattern) {
        try {
            return DateUtils.parseDate(dateStr, pattern);
        } catch (ParseException e) {
            log.error("parseDate报错，dateStr={}, pattern={}", dateStr, pattern);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 比较时间格式为T和Z 的日期是否相同 (2023-12-14T00:00:00Z | 2023-12-14T00:00:00
     * @param dateStr1
     * @param dateStr2
     * @return
     */
    public static Boolean compareDateWithString (String dateStr1, String dateStr2) {
        // 解析带有T和Z的时间格式
        LocalDateTime t1 = LocalDateTime.parse(dateStr1,DateTimeFormatter.ISO_DATE_TIME);
        LocalDateTime t2 = LocalDateTime.parse(dateStr2,DateTimeFormatter.ISO_DATE_TIME);

        return t1.equals(t2);
    }
    /**
     * 日期转日期字符串
     */
    public static String formatDate(Date date, String pattern) {
        return DateFormatUtils.format(date, pattern);
    }

    // 增加指定天数
    public static Date addDays(Date date, int days) {
        if (date == null) {
            return null;
        }
        Calendar instance = Calendar.getInstance();
        // 指定当前日期
        instance.setTime(date);
        instance.add(Calendar.DATE,days);
        return instance.getTime();
    }
    /**
     * 计算某一天是周几
     */
    public static Integer dayForWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int dayForWeek;
        if(c.get(Calendar.DAY_OF_WEEK) == 1){
            dayForWeek = 7;
        }else{
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    /**
     * 获取指定天的开始时间
     *
     * @return
     */
    public static Date beginOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        setMinTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定天的结束时间
     *
     * @return
     */
    public static Date endOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        setMaxTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 设置当天的开始时间
     *
     * @param calendar
     */
    private static void setMinTimeOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    /**
     * 设置当天的结束时间
     *
     * @param calendar
     */
    private static void setMaxTimeOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.MILLISECOND, 999);
    }

    /**
     * 得到昨天的日期
     */
    public static String getYesterday(String dateStr) {
        LocalDate date = LocalDate.parse(dateStr);
        // 计算昨天的日期
        LocalDate yesterday = date.minusDays(1);
        // 将日期转换为字符串
        return yesterday.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * 计算天数差
     */
    public static Long countDay(String date1, String date2) {
        LocalDate d1 = LocalDate.parse(date1);
        LocalDate d2 = LocalDate.parse(date2);

        // 将日期转换为字符串
        return ChronoUnit.DAYS.between(d1, d2);
    }

}
